package com.example.antdback.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 分布式锁redisson配置
 *
 * @author ：lijianguo
 * @date ：Created in 2020/11/7 14:16
 */
@Configuration
public class RedisDistributedLockerConfig {
    @Value("${spring.redis.host:}")
    private String host;
    @Value("${spring.redis.port:}")
    private String port;
    @Value("${spring.redis.password:}")
    private String password;
    @Value("${spring.redis.cluster.nodes:}")
    private String[] nodes;

    @Bean
    public RedissonClient getRedisson(){
        RedissonClient redisson;
        Config config = new Config();
        //集群
        if(nodes != null && nodes.length >0) {
            for(int i=0;i<nodes.length;i++){
                nodes[i] = "redis://"+nodes[i];
            }
            config.useClusterServers()
                    //设置集群状态扫描时间
                    .setScanInterval(2000)
                    .setPingConnectionInterval(60000)
                    .addNodeAddress(nodes).setPassword(password);
        } else {
            config.useSingleServer().setAddress("redis://"+host+":" + port).setPassword(password).setPingConnectionInterval(60000);
        }
        redisson = Redisson.create(config);
        return redisson;
    }

}
